import 'package:dulu/config/global.dart';
import 'package:dulu/manager/main_manager.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MoodLineChartPage extends StatefulWidget {
  MoodLineChartPage({Key key, this.year, this.month, this.day})
      : super(key: key);
  final int year;
  final int month;
  final int day;
  @override
  _MoodLineChartPageState createState() => _MoodLineChartPageState();
}

class _MoodLineChartPageState extends State<MoodLineChartPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 35, right: 35),
      child: MoodLineChart(year: widget.year, month: widget.month),
    );
  }
}

class MoodLineChart extends StatefulWidget {
  MoodLineChart({Key key, this.year, this.month}) : super(key: key);
  final int year;
  final int month;
  @override
  _MoodLineChartState createState() => _MoodLineChartState();
}

class _MoodLineChartState extends State<MoodLineChart> {
  List<Color> gradientColors = [
    Global.sadColor,
    Global.generalColor,
    Global.happyColor,
  ];

  // 判断当前年份是否是闰年
  bool leap(year) {
    return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
  }

  List getEveryDay(y, m, queryMoodFromDate) {
    List ms = [31, leap(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    List data = [];
    for (var i = 1; i < ms[m - 1] + 1; i++) {
      var dateData = queryMoodFromDate(y, m, i);
      if (dateData == null) {
        data.add(0);
      } else {
        data.add(dateData['mood']);
      }
    }
    print(data);
    return data;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          height: 300,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(18)),
            color: Color(0xFFFFFFFF),
          ),
          child: LineChart(mainData()),
        ),
      ],
    );
  }

  LineChartData mainData() {
    MainManager _mainManager = Provider.of<MainManager>(context);
    var data =
        getEveryDay(widget.year, widget.month, _mainManager.queryMoodFromDate);
    return LineChartData(
      titlesData: FlTitlesData(show: false),
      borderData: FlBorderData(show: false),
      minX: 0,
      maxX: data.length.toDouble() - 1,
      minY: 0,
      maxY: 1.5,
      lineBarsData: [
        LineChartBarData(
          spots: List.generate(data.length, (i) {
            return FlSpot(i.toDouble(), data[i].toDouble());
          }),
          isCurved: false,
          colors: gradientColors,
          barWidth: 1,
          dotData: FlDotData(show: false),
          gradientFrom: Offset(1, 1),
          gradientTo: Offset(1, 0),
          belowBarData: BarAreaData(
            show: true,
            gradientFrom: Offset(1, 1),
            gradientTo: Offset(1, 0),
            colors:
                gradientColors.map((color) => color.withOpacity(0.3)).toList(),
          ),
        ),
      ],
    );
  }
}
